package com.qd.p2p.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ReceivingInfoService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.dal.checkrule.ScoreBaseInfoRuleCheck;
import com.qdlc.p2p.dal.common.RuleCheckHelper;
import com.qdlc.p2p.dal.dto.ReceivingInfo;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.ReceivingInfoModel;
import com.qdlc.p2p.dal.util.UserTypeUtils;

/**
 * 收货地址信息
 */
public class ReceivingInfoAction extends BaseAction<ReceivingInfoModel> implements ModelDriven<ReceivingInfoModel> {
	
	@Resource
	private ReceivingInfoService receivingInfoService;

	private Map<String, Object> data;
	
	/** 积分基本规则 **/
	private ScoreBaseInfoRuleCheck scoreBaseInfoRuleCheck = RuleCheckHelper.getValue(ScoreBaseInfoRuleCheck.class);
	
	/**
	 * 跳转收货信息页面
	 * @return
	 */
	@Action(value = "/member/score/receivingInfo",results = { @Result(name = "receivingInfo", type = "ftl", location = "/member/score/receivingInfo.html"),
			@Result(name = "receivingInfo_firm", type = "ftl", location = "/member_borrow/score/receivingInfo.html")})
	public String receivingInfo() {
		User user = getSessionUser();
		saveToken("address_add_token");
		List<ReceivingInfo> list = receivingInfoService.list(getSessionUser());
		request.setAttribute("list", list);
		request.setAttribute("max_num", scoreBaseInfoRuleCheck.recinfo_max_num);// 最大保留条数
		request.setAttribute("exit_num", list.size());// 已经保存条数
		request.setAttribute("remain_num", scoreBaseInfoRuleCheck.recinfo_max_num - list.size());// 还可以保留条数
		if (UserTypeUtils.isBorrowOrInverstment(user.getUserId())) {
			return "receivingInfo_firm";
		}
		return "receivingInfo";
	}
	
	/**
	 * 跳转修改收货信息页面
	 */
	@Action(value = "/member/score/receivingInfo/edit")
	public String edit() {
		return "edit";
	}

	/**
	 * 保存收货记录信息页面
	 * @return
	 */
	@Action(value = "/member/score/receivingInfo/doAppend")
	public void doAppend() throws Exception {
		checkToken("address_add_token");
		model.setUserId(getSessionUser().getUserId());
		receivingInfoService.save(model.prototypeForAppend());
		printWebSuccess();
	}

	/**
	 * 获取收货信息详情
	 */
	@Action(value = "/member/score/receivingInfo/detail")
	public void detail() throws IOException {
		ReceivingInfo receivingInfo = receivingInfoService.find(model.getId());
		data = new HashMap<String, Object>();
		data.put("data", receivingInfo);
		printWebJson(getStringOfJpaObj(data));
	}

	/**
	 * 修改
	 */
	@Action(value = "/member/score/receivingInfo/doModify")
	public void doModify() throws Exception {
		model.setUserId(getSessionUser().getUserId());
		receivingInfoService.update(model.prototype());
		printWebSuccess();
	}

	/**
	 * 设置默认
	 */
	@Action(value = "/member/score/receivingInfo/setDefult")
	public void setDefult() throws Exception {
		model.setUserId(getSessionUser().getUserId());
		receivingInfoService.setDefultReceivingInfo(model);
		printWebSuccess();
	}
	
	/**
	 * 删除/禁用
	 */
	@Action(value = "/member/score/receivingInfo/disable")
	public void disable() throws IOException {
		receivingInfoService.disable(model.getId(), getSessionUser().getUserId());
		printWebSuccess();
	}
	
	/**
	 * 得到收货信息列表
	 * @throws Exception
	 */
	@Action(value = "/member/score/getReceivingInfos")
	public void getReceivingInfo() throws Exception {
		List<ReceivingInfoModel> list = receivingInfoService.ListModel(getSessionUser());
		data = new HashMap<String, Object>();
		data.put("data", list);
		data.put("max_num", scoreBaseInfoRuleCheck.recinfo_max_num);// 最大保留条数
		data.put("exit_num", list.size());// 已经保存条数
		data.put("remain_num", scoreBaseInfoRuleCheck.recinfo_max_num - list.size());// 还可以保留条数
		printWebJson(getStringOfJpaObj(data));
	}

}
